<?php

class BrandAction extends BaseAction
{
	function __construct()
	{
		parent::__construct();
		$this->menuid = !empty($_GET['menuid']) ? intval($_GET['menuid']) : 9;
	}

    function Index()
    {
    	$brand_mod = D('brand');
    	import("ORG.Util.Page");
    	$count = $brand_mod->count();
		$p = new Page($count,5);
		$key = 1;
		$brand_list = $brand_mod->limit($p->firstRow.','.$p->listRows)->findAll();
		foreach($brand_list as $k=>$val){
			$brand_list[$k]['key'] = ($p->nowPage-1) * ($p->listRows) + $key++;
		}
		$sub_menu = $this->admin_menu($this->menuid,true);
		$big_menu = array('javascript:window.top.art.dialog({id:\'add\',iframe:\'?m=brand&a=add\', title:\''.L('add_brand').'\', width:\'470\', height:\'260\', lock:true}, function(){var d = window.top.art.dialog({id:\'add\'}).data.iframe;var form = d.document.getElementById(\'dosubmit\');form.click();return false;}, function(){window.top.art.dialog({id:\'add\'}).close()});void(0);', L('add_brand'));
		$page = $p->show();
		$this->assign('page',$page);
    	$this->assign('sub_menu',$sub_menu);
    	$this->assign('big_menu',$big_menu);
		$this->assign('brand_list',$brand_list);
    	$this->display();
    }

    function add()
    {
    	$this->display();
    }

    function delete()
    {
		$brand_mod = D('brand');
		if((!isset($_GET['id']) || empty($_GET['id'])) && (!isset($_POST['id']) || empty($_POST['id']))) {
            $this->error('请选择要删除的品牌！');
		}
		if( isset($_POST['id'])&&is_array($_POST['id']) ){
			$brand_ids = implode(',',$_POST['id']);
			$brand_mod->delete($brand_ids);
		}else{
			$brand_id = intval($_GET['id']);
		    $brand_mod->where('id='.$brand_id)->delete();
		}
		$this->success(L('operation_success'));
    }

    function edit()
    {
		$brand_mod = D('brand');
		if( isset($_GET['id']) ){
			$brand_id = isset($_GET['id']) && intval($_GET['id']) ? intval($_GET['id']) : $this->error('请选择要编辑的品牌');
		}
		$brand_info = $brand_mod->where('id='.$brand_id)->find();
		$this->assign('brand_info',$brand_info);
		$this->display();

    }

    function update()
    {
    	$brand_mod = D('brand');
    	$data = $brand_mod->create();
    	if (!empty($_FILES['logo']['name'])) {
	        $data['logo'] = $this->_upload();
	    }
	    //dump($data);exit;
		$result = $brand_mod->where('id='.$_POST['id'])->save($data);
		if(false !== $result){
			$this->success(L('operation_success'), '', '', 'edit');
		}else{
			$this->error(L('operation_failure'));
		}
    }

    function insert()
    {
    	$brand_mod = D('brand');
    	$data = array();
    	$brand_name = isset($_POST['name']) && trim($_POST['name']) ? trim($_POST['name']) : $this->error(L('input').L('brand_name'));
    	$brand_info = isset($_POST['info']) && trim($_POST['info']) ? trim($_POST['info']) : $this->error(L('input').L('brand_info'));
    	$exist = $brand_mod->where("name='".$brand_name."'")->count();

    	if($exist != 0){
    		$this->error('该品牌已经存在');
    	}
	    $data['name'] = $brand_name;
	    $data['info'] = $brand_info;
	    $data['status'] = $_POST['status'];
	    if ($_FILES['logo']['name']=='') {
	           $this->error('请添加品牌LOGO');
	    }else{
	        $data['logo'] = $this->_upload();
	    	}
    	$brand_mod->add($data);
    	$this->success(L('operation_success'), '', '', 'add');
    }

    function _upload()
    {
    	import("ORG.Util.UploadFile");
        $upload = new UploadFile();
        //设置上传文件大小
        $upload->maxSize = 3292200;
        $upload->allowExts = explode(',', 'jpg,gif,png,jpeg');
        $upload->savePath = './Public/Uploads/';
        //设置需要生成缩略图，仅对图像文件有效
        $upload->thumb = true;
        // 设置引用图片类库包路径
        //$upload->imageClassPath = '@.ORG.Image';
        //设置需要生成缩略图的文件后缀
        $upload->thumbPrefix = 'm_,s_';  //生产2张缩略图
        //设置缩略图最大宽度
        $upload->thumbMaxWidth = '100';
        //设置缩略图最大高度
        $upload->thumbMaxHeight = '33';
        //设置上传文件规则
        $upload->saveRule = uniqid;
        //删除原图
        $upload->thumbRemoveOrigin = true;
        if (!$upload->upload()) {
            //捕获上传异常
            $this->error($upload->getErrorMsg());
        } else {
            //取得成功上传的文件信息
            $uploadList = $upload->getUploadFileInfo();
            import("ORG.Util.Image");
            Image::thumb($uploadList[0]['savepath'].'i_'.$uploadList[0]['savename'],'m_'.$uploadList[0]['savename'],'',120,40,true);
            $_POST['image'] = $uploadList[0]['savename'];
        }
        $logo = $_POST['image'];
        //$data['create_time'] = time();
        return $logo;
    }

}
?>